package com.jing.weibo.ws.sina;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import weibo4j.Friendships;
import weibo4j.model.User;
import weibo4j.model.UserWapper;
import weibo4j.model.WeiboException;
import weibo4j.org.json.JSONObject;

import com.jing.system.comm.contants.WeiboCons;

/**
 * Friendships[用户粉丝/关注等]新浪微博的接口
 * @author jing.yue
 * @version 2012/08/29 1.0.0
 */
@Component
public class FriendshipsService {

	/**
	 * 根据uid获取用户的粉丝
	 * @param uid
	 * @param count
	 * @param cursor
	 * @param token			授权的token
	 * @return
	 * @throws WeiboException
	 */
	public UserWapper getFollowersById(String uid, Integer count, Integer cursor, String token) throws WeiboException {
		Friendships friendships = new Friendships();
		friendships.setToken(token);
		return friendships.getFollowersById(uid, count, cursor);
	}

	/**
	 * 根据uid获取用户所有的粉丝
	 * @param uid
	 * @param token			授权的token
	 * @return
	 * @throws WeiboException
	 */
	public List<UserWapper> getFollowersByIdAll(String uid, String token) throws WeiboException {
		Friendships friendships = new Friendships();
		friendships.setToken(token);
		List<UserWapper> list = new ArrayList<UserWapper>();
		UserWapper userWapper = friendships.getFollowersById(uid, WeiboCons.WEIBO_USER_PAGE_SIZE, 0);
		list.add(userWapper);
		if(userWapper.getNextCursor() > 0l) {
			while(true) {
				userWapper = friendships.getFollowersById(uid, WeiboCons.WEIBO_USER_PAGE_SIZE, Long.valueOf(userWapper.getNextCursor()).intValue());
				//不存在数据
				if(userWapper == null || userWapper.getUsers() == null || userWapper.getUsers().size() == 0) {
					break;
				}
				//不存在下一页
				else if(userWapper.getNextCursor() <= 0l) {
					list.add(userWapper);
					break;
				}
				else {
					list.add(userWapper);
				}
			}
		}
		return list;
	}

	/**
	 * 根据uid获取用户关注列表
	 * @param uid
	 * @param count
	 * @param cursor
	 * @param token			授权的token
	 * @return
	 * @throws WeiboException
	 */
	public UserWapper getFriendsByID(String uid, Integer count, Integer cursor, String token) throws WeiboException {
		Friendships friendships = new Friendships();
		friendships.setToken(token);
		return friendships.getFriendsByID(uid, count, cursor);
	}

	/**
	 * 根据uid获取用户所有的关注列表
	 * @param uid
	 * @param token			授权的token
	 * @return
	 * @throws WeiboException
	 */
	public List<UserWapper> getFriendsByIDAll(String uid, String token) throws WeiboException {
		Friendships friendships = new Friendships();
		friendships.setToken(token);
		List<UserWapper> list = new ArrayList<UserWapper>();
		UserWapper userWapper = friendships.getFriendsByID(uid, WeiboCons.WEIBO_USER_PAGE_SIZE, 0);
		list.add(userWapper);
		if(userWapper.getNextCursor() > 0l) {
			while(true) {
				userWapper = friendships.getFriendsByID(uid, WeiboCons.WEIBO_USER_PAGE_SIZE, Long.valueOf(userWapper.getNextCursor()).intValue());
				//不存在数据
				if(userWapper == null || userWapper.getUsers() == null || userWapper.getUsers().size() == 0) {
					break;
				}
				//不存在下一页
				else if(userWapper.getNextCursor() <= 0l) {
					list.add(userWapper);
					break;
				}
				else {
					list.add(userWapper);
				}
			}
		}
		return list;
	}

	/**
	 * 取消关注一个用户
	 * @param uid
	 * @param token			授权的token
	 * @throws WeiboException
	 */
	public User destroyFriendshipsDestroyById(String uid, String token) throws WeiboException {
		Friendships friendships = new Friendships();
		friendships.setToken(token);
		return friendships.destroyFriendshipsDestroyById(uid);
	}

	/**
	 * 关注一个用户
	 * @param uid
	 * @param token
	 * @throws WeiboException
	 */
	public User createFriendshipsById(String uid, String token) throws WeiboException {
		Friendships friendships = new Friendships();
		friendships.setToken(token);
		return friendships.createFriendshipsById(uid);
	}

	/**
	 * 获取两个用户之间的详细关注关系情况
	 * @param sourceId	源用户的UID
	 * @param targetId	目标用户的UID
	 * @param token		授权的token
	 * @return
	 * @throws WeiboException
	 */
	public JSONObject showFriendship(String sourceId, String targetId, String token) throws WeiboException {
		Friendships friendships = new Friendships();
		friendships.setToken(token);
		return friendships.showFriendship(sourceId, targetId);
	}
}
